﻿using System.Text;

namespace FireChat.Share.Logger.Printer;


/// <summary>
/// 控制台日志打印器
/// </summary>
public class ConsoleLogPrinter : ILogPrinter
{
    /// <summary>
    /// 记录时间
    /// </summary>
    public required DateTime LogTime { get; init; }

    /// <summary>
    /// 日志等级
    /// </summary>
    public required LogLevel Level { get; init; }

    /// <summary>
    /// 日志消息
    /// </summary>
    public required string Message { get; init; }

    /// <summary>
    /// 日志异常
    /// </summary>
    public Exception? Exception { get; init; }


    /// <summary>
    /// 打印日志
    /// </summary>
    public void Print()
    {
        StringBuilder sb = new();

        sb.AppendLine($"[{LogTime:yyyy-MM-dd HH:mm:ss}][{Level}] >>> {Message}");

        if(Exception is not null)
        {
            if (Level >= LogLevel.Fatal)
            {
                sb.AppendLine(Exception.ToString());
            }
        }

        Console.Write(sb.ToString());
    }
}
